package com.shuoyue.ycgk.views;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.SectionType;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.XToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lombok.Data;


/**
 * 带确认取消按钮的Dialog
 * Created by Forty'7 on 2019/6/17.
 */

public class RegistSpecialCourseDialog extends Dialog {


    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.time)
    Spinner time;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.typerecycleView)
    RecyclerView typerecycleView;
    @BindView(R.id.lay_type)
    LinearLayout layType;

    String timeStr;

    private Context mContext;
    TypeAdapter adapter;
    RegistCallback callBack;

    public RegistSpecialCourseDialog(@NonNull Context context, RegistCallback callBack) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.callBack = callBack;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = View.inflate(context, R.layout.dialog_regist_special_course, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        getWindow().setLayout((SizeUtil.getScreenWidth(mContext) * 3) / 4, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);
        initView();
    }

    void initView() {
        time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] languages = mContext.getResources().getStringArray(R.array.special_course_connect_time);
                timeStr = languages[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        typerecycleView.setLayoutManager(new FlexboxLayoutManager(mContext));
        adapter = new TypeAdapter(getStrs());
        typerecycleView.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            SelectType item = (SelectType) adapter.getItem(position);
            if (item.isSelect) {
                item.setSelect(false);
            } else {
                List<SelectType> selectTypes = adapter.getSelect();
                if (selectTypes.size() < 3) {
                    item.setSelect(true);
                } else {
                    XToast.toast(mContext, "最多选择3个");
                }
            }
            adapter.notifyDataSetChanged();

        });
    }

    @OnClick({R.id.type, R.id.tv_cancel, R.id.tv_ok, R.id.tv_cancel2, R.id.tv_ok2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.type:
                layType.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_ok:
                String nameStr = name.getText().toString();
                String phoneNumStr = phone.getText().toString();
                String typeStr = type.getText().toString();

                if (TextUtils.isEmpty(nameStr)) {
                    XToast.toast(mContext, "请输入学员姓名");
                    return;
                }
                if (TextUtils.isEmpty(phoneNumStr)) {
                    XToast.toast(mContext, "请输入联系电话");
                    return;
                }
                if (TextUtils.isEmpty(timeStr)) {
                    XToast.toast(mContext, "请选择联系时间");
                    return;
                }
                if (TextUtils.isEmpty(typeStr)) {
                    XToast.toast(mContext, "请选择类型");
                    return;
                }
                if (callBack != null) {
                    callBack.setStr(nameStr,
                            phoneNumStr,
                            timeStr, typeStr);
                    dismiss();
                }
                break;
            case R.id.tv_cancel2:
                layType.setVisibility(View.GONE);
                break;
            case R.id.tv_ok2:
                List<SelectType> selectTypes = adapter.getSelect();
                StringBuilder sb = new StringBuilder();
                for (SelectType s : selectTypes) {
                    if (sb.length() != 0) {
                        sb.append(",");
                    }
                    sb.append(s.getName());
                }
                type.setText(sb.toString());
                layType.setVisibility(View.GONE);
                break;
        }
    }

    private List<SelectType> getStrs() {
        List<SelectType> strings = new ArrayList<>();
        strings.add(new SelectType("事业单位"));
        strings.add(new SelectType("公务员省考"));
        strings.add(new SelectType("公务员国考"));
        strings.add(new SelectType("教师招聘"));
        strings.add(new SelectType("医疗卫生"));
        strings.add(new SelectType("三支一扶"));
        strings.add(new SelectType("选调"));
        strings.add(new SelectType("公安"));
        strings.add(new SelectType("法检"));
        strings.add(new SelectType("军队文职"));
        strings.add(new SelectType("军转干"));
        strings.add(new SelectType("国企（烟草/水电气等）"));
        strings.add(new SelectType("银行"));
        strings.add(new SelectType("村官"));
        strings.add(new SelectType("遴选"));
        strings.add(new SelectType("社区"));
        return strings;

    }

    public interface RegistCallback {
        void setStr(String name, String phone, String timeStr, String type);
    }

    class TypeAdapter extends AppBaseQuickAdapter<SelectType> {

        public TypeAdapter(@Nullable List<SelectType> data) {
            super(R.layout.item_specialtype, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, SelectType item) {
            helper.setText(R.id.text, item.getName());
            helper.addOnClickListener(R.id.text);
            helper.setBackgroundRes(R.id.text, item.isSelect ? R.drawable.bg_corner4_blue_stroc1 : R.drawable.bg_corner4_gray_stroc1);
            helper.setTextColor(R.id.text, ContextCompat.getColor(mContext, item.isSelect ?
                    R.color.themeColor :
                    R.color.text_sub_title));
        }

        public List<SelectType> getSelect() {
            List<SelectType> selectTypes = new ArrayList<>();
            for (SelectType s : mData) {
                if (s.isSelect) {
                    selectTypes.add(s);
                }
            }
            return selectTypes;
        }
    }

    @Data
    class SelectType {
        public SelectType(String name) {
            this.name = name;
        }

        String name;
        boolean isSelect;
    }

}
